package cn.tedu.review;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/*复写实现接口方式的售票案例*/
public class TicketRunnable {
    public static void main(String[] args) {
        TicketR ticket = new TicketR();
//        Thread t1 = new Thread(ticket);
//        Thread t2 = new Thread(ticket);
//        Thread t3 = new Thread(ticket);
//        Thread t4 = new Thread(ticket);
//        t1.start();
//        t2.start();
//        t3.start();
//        t4.start();
        /*Executors是用来辅助创建线程池的工具类
        * 常用方法:
        * newFixedThreadPool(int)这个方法可以创建指定数目线程的线程池对象
        * 创建出来的线程池对象就是ExecutorService,负责:新建/启动/销毁 线程
        * */
        //使用Executors工具创建一个最多有5个线程的线程池对象ExecutorService
        ExecutorService pool = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            /*execute()让线程池中的线程来执行业务,每次调用都会将一个线程加入就绪队列*/
            pool.execute(ticket);/*本方法的参数就是你要执行的业务,也就是目标业务类对象*/
        }
    }
}
class TicketR implements Runnable{
    int tivkets = 100;
    Object o=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (o) {
                if (tivkets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "=" + tivkets--);
                }
                if (tivkets <= 0) break;
            }
        }
    }
}